//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientExecuteLead.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_destination_msg","_die_msg","_shared_destination","_shared_alive","_destination_code","_die_code","_destination","_action_display_timer"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object && _caller == player) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		
		_destination_msg = [_parameters, "DEST_MSG", ""] call CFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", ""] call CFUNC(hashGet);
		_shared_destination = [_parameters, "SHARED_DEST_VAR", ""] call CFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", ""] call CFUNC(hashGet);
		_destination = [_parameters, "DESTINATION", []] call CFUNC(hashGet);
		_destination_code = [_parameters, "DEST_CODE", ""] call CFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", ""] call CFUNC(hashGet);
		
		_obj_var_arrived = format["mission_person_arrived_%1",_action_id];
		_is_arrived = OGETVAR(_object,_obj_var_arrived,false);
		TRACE_2("Unit Var Get",_obj_var_arrived,_is_arrived);
		If ([_caller,"Rescue"] call CFUNC(playerHasRole)) then {
			if (!_is_arrived) then {
				_object setCaptive false;
				_object enableAI "MOVE";
				[_object] joinSilent (group _caller);
				sleep 2;
				_object doMove getPos _caller;
				sleep 0.1;
				_object doFollow _caller;
				(group _caller) setBehaviour "SAFE";
				(group _caller) setSpeedMode "FULL";
				_object doFollow _caller;
				TRACE_2("Unit Joined Group",(group _caller),(group _object));

				// We use global object vars because this action is generic and applies to all missions. We won't remove it.
				_act_id = PGETVAR(QGVAR(TELEPORT_AI_ACTION),-1);
				TRACE_2("Check Action",QGVAR(TELEPORT_AI_ACTION),_act_id);
				if (isnil "_act_id" || _act_id < 0) then {
					_action_title = ["STR_MF_TELEPORT_AI"] call CFUNC(translate);
					_arguments = [];
					_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, -1];
					_arguments set [TMF_EXECUTE_ACTION_PARAM_FUNC, "TeleportAI"];
					_condition = format["((alive _target) AND (({!isplayer _x} count (units (group _target))) > 0) AND (_target == (leader (group _target))))"];
					_act_id = _caller addAction [_action_title, TMF_ADD_ACTION_SCRIPT,_arguments,-10,False,True,"",_condition];
					PSETVAR(QGVAR(TELEPORT_AI_ACTION),_act_id);
					TRACE_2("Added Action",_action_title,_act_id);
				};
				
				// We use global object vars because this action is generic and applies to all missions. We won't remove it.
				_act_id = PGETVAR(QGVAR(RELEASE_AI_ACTION),-1);
				TRACE_2("Check Action",QGVAR(RELEASE_AI_ACTION),_act_id);
				if (isnil "_act_id" || _act_id < 0) then {
					_action_title = ["STR_MF_RELEASE_AI"] call CFUNC(translate);
					_arguments = [];
					_arguments set [TMF_EXECUTE_ACTION_PARAM_ACTID, -1];
					_arguments set [TMF_EXECUTE_ACTION_PARAM_FUNC, "ReleaseAI"];
					_condition = format["((alive _target) AND (({!isplayer _x} count (units (group _target))) > 0) AND (_target == (leader (group _target))))"];
					_act_id = _caller addAction [_action_title, TMF_ADD_ACTION_SCRIPT,_arguments,-10,False,True,"",_condition];
					PSETVAR(QGVAR(RELEASE_AI_ACTION),_act_id);
					TRACE_2("Added Action",_action_title,_act_id);
				};

				[_object,_caller] spawn {
					private ["_person","_leader","_group","_wp1","_wp2"];
					_person = _this select 0;
					_leader = _this select 1;
					// In case the person or the leader dies, we dissociate both
					waituntil {sleep 10; ((group _person != group _leader) OR (!alive _person) OR (!alive _leader))};
					if (alive _person) then {
						doStop _person;
						[_person] joinSilent ([GVAR(own_side)] call CFUNC(createGroup));
						doStop _person;
						_group = group _person;
						_group setBehaviour "STEALTH";
						_group setCombatMode "BLUE";
						_group allowFleeing 1;
						_wp1 = _group addWaypoint [getPos _person, 0];
						_wp1 setWaypointType "MOVE";
						_wp2 = _group addWaypoint [getPos _person, 0];
						_wp2 setWaypointType "CYCLE";
						deleteWaypoint [_group, 0];
						_group setCurrentWaypoint _wp1;
						_person doMove (getPos _person);
						TRACE_1("AI Degrouped",ARR_3(_person, group _person, count (waypoints _group)));
					};
					TRACE_1("Exited Group Verification",ARR_3(_person, typeof _person, group _person));
				};
			};
		} else {
			[(["STR_MF_LEAD_NOTRESCUE"] call CFUNC(translate))] call CFUNC(globalChat);
		};
	};
};

TRACE("End");
TMFPROFILERSTOP;